﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;

namespace UnitTest
{
    public interface IExtensionNanager
    {
        Boolean CheckExtension(string fileName);
    }

    public class ExtensionManager : IExtensionNanager
    {
        public bool CheckExtension(string fileName)
        {
            //Some complex business logic might goes here. May be DB operation or file system handling  
            return false;
        }
    }

    //Stub implementation to bypass actual Extension manager class.  
    public class StubExtensionManager : IExtensionNanager
    {
        public bool CheckExtension(string fileName)
        {
            return true;
        }
    }

    public class FileChecker
    {
        IExtensionNanager objmanager = null;
        //Default constructor  
        public FileChecker()
        {
            objmanager = new ExtensionManager();
        }
        //parameterized constructor  
        public FileChecker(IExtensionNanager tmpManager)
        {
            objmanager = tmpManager;
        }

        public Boolean CheckFile(String FileName)
        {
            return objmanager.CheckExtension(FileName);
        }
    }

    [TestFixture]
    public class UnitTest1
    {
        [Test]
        public void TestWithStub()
        {
            //Act  
            StubExtensionManager stub = new StubExtensionManager();
            FileChecker checker = new FileChecker(stub);

            //Action  
            bool IsTrueFile = checker.CheckFile("myFile.whatever");

            //Assert  
            Assert.AreEqual(true, IsTrueFile);
        }

        [Test]
        public void TestWithMock()
        {
            //Act  
            var mockObj = new Mock<IExtensionNanager>();
            mockObj.Setup(x => x.CheckExtension(It.IsAny<string>())).Returns(true);
            FileChecker checker = new FileChecker(mockObj.Object);

            //Action  
            bool isTrueFile = checker.CheckFile("myFile.whatever");

            //Assert  
            Assert.AreEqual(true, isTrueFile);
            mockObj.Verify();
        }

    }   
}
